﻿using System.Configuration;
using System.Data.Linq;

namespace Chowpoo.Sql
{
    public class DataContext<T> : DataContext where T : class
    {
        public DataContext(string name)
            : base(ConfigurationManager.ConnectionStrings[name].ConnectionString)
        {
            SourceTable = GetTable<T>();
        }

        public Table<T> SourceTable { get; private set; }

        public static DataContext<T> CreateReadonlyContext(string name)
        {
            return new DataContext<T>(name) { ObjectTrackingEnabled = false };
        }

        public static DataContext<T> CreateContext(string name)
        {
            return new DataContext<T>(name);
        }
    }

    public class DataContext<T1, T2> : DataContext
        where T1 : class
        where T2 : class
    {
        public DataContext(string name)
            : base(ConfigurationManager.ConnectionStrings[name].ConnectionString)
        {
            SourceTable1 = GetTable<T1>();
            SourceTable2 = GetTable<T2>();
        }

        public Table<T1> SourceTable1 { get; private set; }
        public Table<T2> SourceTable2 { get; private set; }

        public static DataContext<T1, T2> CreateReadonlyContext(string name)
        {
            return new DataContext<T1, T2>(name) { ObjectTrackingEnabled = false };
        }

        public static DataContext<T1, T2> CreateContext(string name)
        {
            return new DataContext<T1, T2>(name);
        }
    }
}